﻿
namespace Basket.API.Data
{
    public class BasketRepository(IDocumentSession session): IBasketRepository
    {
        public async Task<bool> DeleteBasketAsync(string userName, CancellationToken cancellationToken = default)
        {
            session.Delete<ShoppingCart>( userName);
            await session.SaveChangesAsync(cancellationToken);
            return true;
        }

        public async Task<ShoppingCart> GetBasketAsync(string userName, CancellationToken cancellationToken = default)
        {
            var basket=await session.LoadAsync<ShoppingCart>(userName, cancellationToken);
            return basket is null?throw new NotFoundBasketExpection(userName) :basket;
        }

        public async Task<ShoppingCart> UpdateBasketAsync(ShoppingCart basket, CancellationToken cancellationToken = default)
        {
            session.Store(basket);
            await session.SaveChangesAsync(cancellationToken);
            return basket;

        }
    }
}
